{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(547, 730, 3)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.完成图片加载 2.获取图片信息（例如宽高） 3.通过resize完成缩放 4.检查最终结果\n",
    "import cv2\n",
    "# 加载图片\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "# 获取信息 图片的行数对应高度信息，列数对应宽度信息\n",
    "imgInfo = img.shape\n",
    "print(imgInfo)\n",
    "# 图片的宽度 高度以及颜色组成方式\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "mode = imgInfo[2]\n",
    "# 等比例缩放，原始宽高乘同一个因数\n",
    "dstHeight = int(height*0.5)\n",
    "dstWidth = int(width*0.5)\n",
    "dstImg = cv2.resize(img,(dstWidth,dstHeight))\n",
    "cv2.imshow('imageResize',dstImg)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "最近领域插值法（默认算法）\n",
    "srcImg 10*20 dstImg 5*10\n",
    "src->dst (2,4)->(1,2)\n",
    "通过dstImg的坐标求出srcImg的坐标：secX = dstX*(srcImg行/dstImg行) = 1*（10/5）=2\n",
    "当不能整除时，进行取整 12.3->12\n",
    "代码实现最近领域插值法：1.获取图片信息 2.创建空白模板，模板大小与图片缩放之后相同 3.计算相应位置的像素值\n",
    "'''\n",
    "import numpy as np\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "imgInfo = img.shape\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "mode = imgInfo[2]\n",
    "dstHeight = int(height*0.5)\n",
    "dstWidth = int(width*0.5)\n",
    "# 创建空白模板\n",
    "dstImg = np.zeros((dstHeight,dstWidth,mode),np.uint8)\n",
    "for i in range(0,dstHeight):\n",
    "    for j in range(0,dstWidth):\n",
    "        hNew = int(i*(height*1.0/dstHeight))\n",
    "        wNew = int(j*(width*1.0/dstWidth))\n",
    "        dstImg[i,j] = img[hNew,wNew]\n",
    "cv2.imshow('dstImg',dstImg)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "通过移位实现缩放\n",
    "[[0.5,0,0],[0,0.5,0]] \n",
    "将其拆分为2*2 和 2*1的矩阵\n",
    "matA [[0.5,0],[0,0.5]] \n",
    "matB [[0],[0]]\n",
    "matC 为输入矩阵即[x,y]\n",
    "运算过程：A*C+B=[[0.5*x+0*y],[0*x+0.5*y]]+[[0],[0]]=[[x+0],[y+0]]\n",
    "(10,20)->(5,10)\n",
    "\n",
    "'''\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "cv2.imshow('src',img)\n",
    "imgInfo = img.shape\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "matScale = np.float32([[0.5,0,0],[0,0.5,0]])\n",
    "dstImg = cv2.warpAffine(img,matScale,(int(width/2),int(height/2)))\n",
    "cv2.imshow('dstImg',dstImg)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
